package com.wms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.pojo.User;
import com.wms.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Description:
 * @Project:
 * @CreateDate: Created in 2020/3/18 21:36
 * @Author: YXH
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    UserServiceImpl userService;

    @RequestMapping("/allAdmin")
    public String allAdmin(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "5") Integer pageSize,
                           Model model){
        PageHelper.startPage(pageNum,pageSize);
        List<User> allAdmin = userService.allAdmin();
        PageInfo adminPage = new PageInfo(allAdmin);
        model.addAttribute("adminPage",adminPage);
        return "admin/admin-list";
    }

    @RequestMapping("/setStatus")
    public String setStatus(@RequestParam(name = "ID") String ID){
        int id = Integer.parseInt(ID);
        User user = userService.queryById(id);
        if (user.getStatus()==1){
            user.setStatus(0);
        }else {
            user.setStatus(1);
        }
        userService.setStatus(id,user.getStatus());
        return "redirect:/admin/allAdmin";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){ return "/admin/admin-add";
    }

    @RequestMapping("/addAdmin")
    public String addAdmin(User user){
        userService.addAdmin(user);
        return "redirect:/admin/allAdmin";
    }

    @RequestMapping("/toEdit")
    public String toEdit(@RequestParam(name = "uid")String uid,
                         Model model){
        int id = Integer.parseInt(uid);
        User user = userService.queryById(id);
        model.addAttribute("user",user);
        return "/admin/admin-edit";
    }

    @RequestMapping("/editAdmin")
    public String editAdmin(User user){
        System.out.println(user);
        userService.editAdmin(user);
        return "redirect:/admin/allAdmin";
    }

    @RequestMapping("/delAdmin")
    public String delAdmin(@RequestParam(name = "uid")String uid){
        int id = Integer.parseInt(uid);
        userService.delAdmin(id);
        return "redirect:/admin/allAdmin";
    }
}
